package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.domain.CmsSubjectProductRelation;
import com.itheima.mapper.CmsSubjectProductRelationMapper;
import com.itheima.service.db.CmsSubjectProductRelationService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class CmsSubjectProductRelationServiceImpl implements CmsSubjectProductRelationService {
    @Autowired
    CmsSubjectProductRelationMapper cmsSubjectProductRelationMapper;

    @Override
    public List<CmsSubjectProductRelation> selectByProductId(Integer id) {
        LambdaQueryWrapper<CmsSubjectProductRelation> lqw=new LambdaQueryWrapper<>();
        lqw.eq(CmsSubjectProductRelation::getProductId,id);
        return cmsSubjectProductRelationMapper.selectList(lqw);
    }

    @Override
    public void updateByProductId(Integer id, List<CmsSubjectProductRelation> CmsSubjectProductRelationList) {
        LambdaQueryWrapper<CmsSubjectProductRelation> lqw=new LambdaQueryWrapper<>();
        lqw.eq(CmsSubjectProductRelation::getProductId,id);
        cmsSubjectProductRelationMapper.delete(lqw);

        for (CmsSubjectProductRelation cmsSubjectProductRelation : CmsSubjectProductRelationList) {
            cmsSubjectProductRelation.setProductId(id.longValue());
            cmsSubjectProductRelationMapper.insert(cmsSubjectProductRelation);
        }

    }

    @Override
    public void save(Long productId, List<CmsSubjectProductRelation> subjectProductRelationList) {
        for (CmsSubjectProductRelation cmsSubjectProductRelation : subjectProductRelationList) {
            cmsSubjectProductRelation.setProductId(productId);
            cmsSubjectProductRelationMapper.insert(cmsSubjectProductRelation);
        }
    }
}
